<?php

namespace common\models;

use common\models\base\ArchiveAddonRecord as Base;
use Yii;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsTrait;

/**
 * This is the model class for table "attachment".
 *
 * @property integer $id
 * @property string $archive_id
 * @property integer $type
 * @property string $description
 */
class Attachment extends Base
{
	use SaveRelationsTrait;

	/**
	 * @inheritdoc
	 */
	public static function tableName()
	{
		return 'attachment';
	}

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
//            [['archive_id'], 'required'],
			[['archive_id', 'type'], 'integer'],
			[['description'], 'string'],
		];
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'id' => 'ID',
			'archive_id' => 'Archive ID',
			'type' => 'Type',
			'description' => 'Description',
		];
	}

	public function getArchive()
	{
		return $this->hasOne(\common\models\Archive::className(),['Archive_id'=>'id']);
	}

	public function getUploadFiles()
	{
		return $this->hasMany(UploadFile::className(), ['id' => 'upload_file_id'])
			->viaTable('rel_attachment_upload_file', ['attachment_id' => 'id']);
	}

	public function behaviors()
	{
		return [
			'saveRelate' => [
				'class' => SaveRelationsBehavior::className(),
				'relations' => ['uploadFiles'],
			]
		];
	}
}
